module.exports = function(lab) {
    return {
        searchMembers: function(session, { start = 0, limit = 10, keyword }) {
            if (!session.currentOU || !session.currentUser) {
                throw @lab.errors.not_login;
            }

            var conditions = ["ouid = ?", "is_removed = ?","member_no is not null"];
            var params = [session.currentOU.id, false];

            if (keyword){
                conditions.push("name like ? or phone like ? or email like ? or real_name like ?");
                params.push(`%${keyword}%`, `%${keyword}%`, `${keyword}%`, `%${keyword}%`);
            }

            return lab.query("slime_members", {
                start,
                limit: isNaN(limit) || limit < 0 ? 0 : limit,
                conditions,
                params,
            });
        },
        getMemberIDsInfo: function(session, member_id) {
            if (!session.currentOU || !session.currentUser) {
                throw @lab.errors.not_login;
            }
            return lab.find("slime_member_ids", member_id)
        },
        getMemberUpdates: function(session, member_id) {
            if (!session.currentOU || !session.currentUser) {
                throw @lab.errors.not_login;
            }
            return lab.query("slime_member_updates", {
                conditions: ["member_id = ?"],
                params: [member_id],
                sort: "create_date desc"
            });
        },
        getMemberSources : function (session) {
            if (!session.currentOU || !session.currentUser) {
                throw @lab.errors.not_login;
            }
            return lab.slime.member.@getSourcesByOUID(session.currentOU.id);
        }
    }
}